package com.ts.proj.service.impl;

import com.ts.base.service.impl.CommonServiceImpl;
import com.ts.proj.entity.SystemMenuEntity;
import com.ts.proj.repository.SystemMenuRepository;
import com.ts.proj.service.SystemMenuService;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SystemMenuServiceImpl extends CommonServiceImpl<SystemMenuEntity> implements SystemMenuService {

    @Autowired
    private SystemMenuRepository systemMenuRepository;


    @Override
    public List<SystemMenuEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<SystemMenuEntity> sp = this.getSpecification(pageData,SystemMenuEntity.class);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return systemMenuRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count(PageData pageData) {
        Specification<SystemMenuEntity> sp = this.getSpecification(pageData,SystemMenuEntity.class);
        return systemMenuRepository.count(sp);
    }

    @Override
    public boolean deleteByIds(Iterable<SystemMenuEntity> iterable) {
        systemMenuRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<SystemMenuEntity> saveAll(List<SystemMenuEntity> systemMenuEntities) {
        return systemMenuRepository.saveAll(systemMenuEntities);
    }

    @Override
    public boolean deleteAll() {
        return false;
    }

    @Override
    public List<SystemMenuEntity> findAllById(Integer[] ids) {
        List<SystemMenuEntity> list = systemMenuRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            SystemMenuEntity systemMenuEntity = new SystemMenuEntity();
            List<SystemMenuEntity> result = new ArrayList<>();
            result.add(systemMenuEntity);
            return result;
        }
        return list;
    }
}
